<?php
class User {
	// Déclarations des données
	var $id = NULL;
	var $nickname = NULL;
	var $mail = NULL;
	var $password = NULL;
	var $subscribeDate = NULL;
	var $options = Array();
	
	// Constructeur
	function User($pValue = "", $pMethod = "id"){
		switch ($pMethod){
			case "id" :
				$this->LoadFromID($pValue);
				break;
			case "mail" :
				$this->LoadFromMail($pValue);
				break;
		}
	}
	
	
	function LoadFromID($pId){
		global $wpdb;
		
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM users WHERE id='%d' ", $pId));
		$this->extractRowInformation($_row);
	}
	
	function LoadFromMail($pMail){
		global $wpdb;
		
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM users WHERE mail='%s' ", $pMail));
		$this->extractRowInformation($_row);
	}
	
	function extractRowInformation($pRow){
		global $wpdb;
		
		$_row = $pRow;
		$this->id = $_row->id;
		$this->nickname = $_row->nickname;
		$this->mail = $_row->mail;
		$this->password = $_row->password;
		$this->subscribeDate = $_row->subscribedate;
		
		// recuperation des options
		$_options = $wpdb->get_results($wpdb->prepare("SELECT * from useroptions WHERE fk_user = %d", $this->id));
		foreach($_options as $_option){ 
			$this->options[$_option->name] = $_option->value;
		}
	}
	
	// Déclarations des méthodes
	
	//======================
	// Création
	//======================
	
	function create($pNickname, $pMail, $pPassword){
		global $wpdb;
		
		// creation de l'utilisateur
		$queryTable = 'users';
		$queryData = array( 
			'nickname' => $pNickname, 
			'mail' => $pMail,
			'password' => md5($pPassword),
			'subscribedate' => date("Y-m-d"),
			'activated' => 0
		);
		$queryDataFormat = array('%s','%s','%s','%s');
		$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
		
		$this->LoadFromID($wpdb->insert_id);
		
		// creation des options utilisateur de base
		$this->setOption("displayTicketInFeed", "enable");
		$this->setOption("displayStatusInFeed", "enable");
		$this->setOption("displayRelationshipInFeed", "enable");
		$this->setOption("displayNewsInFeed", "enable");
		
		// creation du code d'activation du compte
		$queryTable = 'useractivations';
		$queryData = array( 
			'fk_user' => $this->id, 
			'code' => md5($pNickname.$pMail.$pPassword),
		);
		$queryDataFormat = array('%d','%s');
		$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
		 
		return $this->id;
	}

	//======================
	// Get/Set
	//======================
	
	function getID(){
		return $this->id;
	}
	
	function getPassword(){
		return $this->password;
	}
	
	function getNickname(){
		return $this->nickname;
	}
	
	function getMail(){
		return $this->mail;
	}
	
	function setOption($pName,$pValue){
		global $wpdb;
		$queryTable = 'useroptions';
		$queryData = array( 
			'fk_user' => $this->id, 
			'name' => $pName,
			'value' => $pValue
		);
		$queryType = array('%d','%s','%s');
		$wpdb->insert( $queryTable, $queryData, $queryType);
	}
	
	function getOption($pOptionName){
		return $this->options[$pOptionName];
	}

	function getMostCommonVisitors()
	{
	}

	function getMostVisitedProfiles()
	{
	}

	function getRelationship()
	{
	}

	function getRelationshipDemands()
	{
	}
}